<template>
  <div class="favorites-list">
    <el-skeleton :loading="loading" :rows="3" animated>
      <template #default>
        <div v-if="favorites.length === 0" class="empty-state">
          <el-empty description="暂无收藏" />
        </div>
        <div v-else class="favorites-grid">
          <div v-for="house in favorites" 
               :key="house.id" 
               class="house-card"
               @click="goToDetail(house.id)">
            <div class="house-image">
              <el-image 
                :src="house.images[0] || defaultImage" 
                fit="cover"
                :preview-src-list="house.images">
                <template #error>
                  <div class="image-error">
                    <el-icon><Picture /></el-icon>
                  </div>
                </template>
              </el-image>
              <div class="status-tag" v-if="house.status === 'available'">
                {{ [2, 3, 4].includes(house.type) ? '可售' : '可租' }}
              </div>
            </div>
            
            <div class="house-info">
              <h3 class="house-title">{{ house.title }}</h3>
              <div class="house-tags">
                <el-tag size="small" type="info">{{ house.house_type }}</el-tag>
                <el-tag size="small" type="success">{{ house.decoration_type === 'medium' ? '精装' : '简装' }}</el-tag>
                <el-tag size="small" type="warning">{{ house.orientation === 'south' ? '朝南' : house.orientation }}</el-tag>
              </div>
              <div class="house-details">
                <span class="area">{{ house.area }}m²</span>
                <span class="floor">{{ house.floor }}/{{ house.total_floor }}层</span>
              </div>
              <div class="house-location">
                <el-icon><Location /></el-icon>
                <span>{{ house.district }} {{ house.address }}</span>
              </div>
              <div class="house-price">
                <span class="price">¥{{ house.price }}</span>
                <span class="unit">{{ getPriceUnit(house.type) }}</span>
                <span class="deposit" v-if="house.type === 1">押金：¥{{ house.deposit }}</span>
              </div>
            </div>
          </div>
        </div>
      </template>
    </el-skeleton>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { Location, Picture } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import axios from 'axios'

const router = useRouter()
const favorites = ref([])
const loading = ref(false)

// 默认图片URL
const defaultImage = 'https://img.freepik.com/free-vector/house-isolated-field_1308-26795.jpg'

const getPriceUnit = (type) => {
  switch (Number(type)) {
    case 1:
      return '/月'
    case 2:
    case 3:
    case 4:
      return '/m²'
    default:
      return '/月'
  }
}

const fetchFavorites = async () => {
  loading.value = true
  try {
    const userId = localStorage.getItem('userid')
    const token = localStorage.getItem('token')
    
    if (!userId || !token) {
      ElMessage.warning('请先登录')
      router.push('/login')
      return
    }

    const response = await axios.get('http://127.0.0.1:8000/users/cart/check/', {
      params: { user: userId },
      headers: {
        'Authorization': token.startsWith('Bearer ') ? token : `Bearer ${token}`
      }
    })

    if (response.data.msg === 'OK') {
      favorites.value = response.data.data || []
    } else {
      ElMessage.warning(response.data.msg || '获取收藏列表失败')
    }
  } catch (error) {
    console.error('获取收藏列表失败:', error)
    if (error.response?.status === 401) {
      ElMessage.warning('请先登录')
      router.push('/login')
    } else {
      ElMessage.error('获取收藏列表失败')
    }
  } finally {
    loading.value = false
  }
}

const goToDetail = (id) => {
  router.push({
    path: '/house-detail',
    query: { id }
  })
}

onMounted(() => {
  fetchFavorites()
})
</script>

<style scoped>
.favorites-list {
  padding: 20px;
}

.empty-state {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 200px;
}

.favorites-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 20px;
}

.house-card {
  background: white;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  cursor: pointer;
  transition: transform 0.2s, box-shadow 0.2s;
}

.house-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
}

.house-image {
  position: relative;
  height: 200px;
}

.house-image :deep(.el-image) {
  width: 100%;
  height: 100%;
}

.image-error {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f5f7fa;
  color: #909399;
}

.status-tag {
  position: absolute;
  top: 12px;
  left: 12px;
  background: rgba(64, 158, 255, 0.9);
  color: white;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
}

.house-info {
  padding: 16px;
}

.house-title {
  margin: 0 0 12px;
  font-size: 16px;
  font-weight: 500;
  line-height: 1.4;
  color: #303133;
}

.house-tags {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}

.house-details {
  display: flex;
  gap: 16px;
  color: #606266;
  font-size: 14px;
  margin-bottom: 12px;
}

.house-location {
  display: flex;
  align-items: center;
  gap: 4px;
  color: #606266;
  font-size: 14px;
  margin-bottom: 12px;
}

.house-price {
  color: #f56c6c;
  display: flex;
  align-items: baseline;
  gap: 4px;
}

.price {
  font-size: 20px;
  font-weight: 500;
}

.unit {
  font-size: 14px;
}

.deposit {
  margin-left: auto;
  font-size: 14px;
  color: #909399;
}

@media (max-width: 768px) {
  .favorites-grid {
    grid-template-columns: 1fr;
  }
  
  .house-image {
    height: 160px;
  }
}
</style> 